//
//  BBDTabbarViewController.m
//  棒棒哒
//
//  Created by rimi on 15/8/3.
//  Copyright (c) 2015年 rimi. All rights reserved.
//

#import "BBDTabbarViewController.h"
#import "BBDNavigationViewController.h"
#import "BBDHealthInformationViewController.h"
#import "BBDHealthKnowledgeViewController.h"
#import "BBDHealthQuestionsViewController.h"
#import "Config.h"

static BBDTabbarViewController *BBDTabbarVC = nil;

//初始化3个静态变量
//拖拽手势起始点
static CGPoint point;
//当前主页面大小
static CGSize  currentSize ;
//当前主页面边偏移距离
static CGFloat endPointX;

@interface BBDTabbarViewController ()<UIGestureRecognizerDelegate>

@property(strong,nonatomic)UIView *sideslipView;

@end



@implementation BBDTabbarViewController

+(BBDTabbarViewController *)shareTabBarController
{
    static dispatch_once_t oneToken;
    dispatch_once(&oneToken, ^{
        BBDTabbarVC = [[BBDTabbarViewController alloc]init];
        BBDTabbarVC.view.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
        //为tabbarcontroller左边设置阴影
        BBDTabbarVC.view.layer.shadowColor = [UIColor blackColor].CGColor;
        BBDTabbarVC.view.layer.shadowOffset = CGSizeMake(3, 3);
        BBDTabbarVC.view.layer.shadowOpacity = 1.0;
        
        
    });
    return BBDTabbarVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    BBDHealthInformationViewController *information = [[BBDHealthInformationViewController alloc]initWithTitle:@"健康资讯" image:nil selectedImage:nil];
    BBDNavigationViewController *informationNav = [[BBDNavigationViewController alloc]initWithRootViewController:information];
    
    BBDHealthKnowledgeViewController *knowledge = [[BBDHealthKnowledgeViewController alloc]
                                                   initWithTitle:@"健康知识" image:nil selectedImage:nil];
    BBDNavigationViewController *knowledgeNav = [[BBDNavigationViewController alloc]initWithRootViewController:knowledge];
    
    BBDHealthQuestionsViewController *questions = [[BBDHealthQuestionsViewController alloc]initWithTitle:@"症状问答" image:nil selectedImage:nil];
    BBDNavigationViewController *questionsNav = [[BBDNavigationViewController alloc]initWithRootViewController:questions];
    
    self.viewControllers = @[informationNav, knowledgeNav, questionsNav];
    
    [self.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize:18]} forState:UIControlStateNormal];
    
    UIView *panView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, 40, SCREEN_HEIGHT - 113)];
    [self.view addSubview:panView];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panTabBarVC:)];
    pan.delegate = self;
    [panView addGestureRecognizer:pan];
    
}

- (void)tabBarMoveToSide
{
    CGFloat duration = ABS(BBDTabbarVC.view.center.x - SCREEN_WIDTH * 1.25)/(SCREEN_WIDTH/2.0 * 1.5) * 0.5 ;
    [UIView animateWithDuration:duration animations:^{
        BBDTabbarVC.view.center = CGPointMake(SCREEN_WIDTH / 2 * 2.5, SCREEN_HEIGHT / 2);
        BBDTabbarVC.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH * 0.9, SCREEN_HEIGHT * 0.9);
     
    }];
    currentSize =  BBDTabbarVC.view.bounds.size;
    endPointX = SCREEN_WIDTH / 2 * 1.4;
}

- (void)tabBarMoveToCenter
{
    CGFloat duration = ABS(BBDTabbarVC.view.center.x - SCREEN_WIDTH / 2)/(SCREEN_WIDTH /2 * 1.5) *0.5 ;
    [UIView animateWithDuration:duration animations:^{
        BBDTabbarVC.view.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
        BBDTabbarVC.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    }];
    currentSize =  BBDTabbarVC.view.bounds.size;
    endPointX = SCREEN_WIDTH / 2 * 1.6;
}

- (void)panTabBarVC:(UIPanGestureRecognizer *)sender
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //当前主页面大小会在拖拽结束后修正
        currentSize =  BBDTabbarVC.view.bounds.size;
        //当前主页面边偏移距离也会在拖拽结束后修正
        endPointX = SCREEN_WIDTH / 2 * 1.6;
    });
    if (sender.state == UIGestureRecognizerStateBegan) {
        point = BBDTabbarVC.view.center;
    }else if (sender.state == UIGestureRecognizerStateChanged) {
        CGPoint translation = [sender translationInView:BBDTabbarVC.view];

        if (BBDTabbarVC.view.center.x >= SCREEN_WIDTH * 0.50 && BBDTabbarVC.view.center.x <= SCREEN_WIDTH * 1.25) {
            BBDTabbarVC.view.center = CGPointMake(translation.x + point.x, SCREEN_HEIGHT / 2);
            BBDTabbarVC.view.bounds = CGRectMake(0, 0, currentSize.width * (1 - 0.1 * translation.x / endPointX), currentSize.height * (1 - 0.1 * translation.x / endPointX));
            [[NSNotificationCenter defaultCenter]postNotificationName:@"bounds" object:BBDTabbarVC.view];
        }

    }else if (sender.state == UIGestureRecognizerStateEnded) {
        if (BBDTabbarVC.view.center.x >= SCREEN_WIDTH ) {
            [self tabBarMoveToSide];
        }else{
            [self tabBarMoveToCenter];
        }
    }
    
    
    
}


- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        UIPanGestureRecognizer *sender = (UIPanGestureRecognizer *)gestureRecognizer;
        CGPoint translation = [sender translationInView:BBDTabbarVC.view];
        if (BBDTabbarVC.view.center.x == SCREEN_WIDTH * 0.50 && translation.x < 0) {
            return NO;
        }else if (BBDTabbarVC.view.center.x == SCREEN_WIDTH * 1.25 && translation.x > 0){
            return NO;
        }else{
            return YES;
        }
    }else{
        return YES;
    }
}




@end
